package net.cloudcodex.client.campaign.presenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.cloudcodex.client.CloudCodexEventBus;
import net.cloudcodex.client.campaign.view.CampaignView;
import net.cloudcodex.client.campaign.view.CampaignViewImpl;
import net.cloudcodex.client.campaign.view.CampaignsView.Message;
import net.cloudcodex.client.campaign.view.panel.CampaignPanel;
import net.cloudcodex.client.cdo.CharacterCDO;
import net.cloudcodex.client.character.presenter.CharacterDescriptionPresenter;
import net.cloudcodex.client.character.presenter.CharactersInCampaignPresenter;
import net.cloudcodex.client.util.rpc.SuccessCallback;
import net.cloudcodex.client.widgets.ToggleButtonGroup;
import net.cloudcodex.shared.dto.NotificationDTO;
import net.cloudcodex.shared.dto.Result;
import net.cloudcodex.shared.dto.campaign.character.CharacterDescriptionDTO;
import net.cloudcodex.shared.dto.campaign.character.CharacterHeaderDTO;
import net.cloudcodex.shared.dto.home.CampaignSummaryDTO;
import net.cloudcodex.shared.rpc.CampaignRPCAsync;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.inject.Inject;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

/**
 * 
 * 
 * @author ADaroussin
 */
@Presenter(view = CampaignViewImpl.class, multiple = true)
public class CampaignPresenter extends
		BasePresenter<CampaignView, CloudCodexEventBus> {
	private final Message message;
	private CampaignSummaryDTO campaignSummaryDTO;
	private final CampaignRPCAsync campaignService;
	private CharactersInCampaignPresenter charactersInCampaignPresenter;

	private Map<Long, CharacterHeaderDTO> characterHeaderDTOsMap = new HashMap<Long, CharacterHeaderDTO>();

	private final Map<String, IsWidget> listContents = new HashMap<String, IsWidget>();
	private List<CharacterHeaderDTO> characterHeaderDTOList = new ArrayList<CharacterHeaderDTO>();
	private String selectedContent;
	private String nickName;
	private CampaignConfigPresenter campaignConfigPresenter;
	private boolean isInit = false ;
	private long userId;

	@Inject
	public CampaignPresenter(Message pMessage, CampaignRPCAsync campaignService) {
		this.message = pMessage;
		this.campaignService = campaignService;
	}

	@Override
	public void bind() {
		this.view.getNotificationListBox().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// ListBox listBox = (ListBox) event.getSource();
				// String text =
				// listBox.getItemText(listBox.getSelectedIndex());
				// System.out.println("test = " + text);

			}
		});

		this.view.getToggleButtonGroup().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				ToggleButtonGroup toggleButtonGroup = (ToggleButtonGroup) event
						.getSource();
				ToggleButton selectedButton = toggleButtonGroup
						.getSelectedButton();
				selectedContent = selectedButton.getTitle();
				refreshContent();
			}
		});
	}

	public void initCampaignView() {
		initNotificationPanel();
		if (campaignSummaryDTO != null) {
			initCharactersInCampaignPanel();
		}
		initCampaignConfigPanel();
		this.isInit =  true;
	}

	private void initCampaignConfigPanel() {
		campaignConfigPresenter = eventBus
				.addHandler(CampaignConfigPresenter.class);
		initCampaignConfigView(campaignConfigPresenter);
		listContents.put(CampaignPanel.CAMPAIGN_CONFIG, campaignConfigPresenter
				.getView().asWidget());
	}

	/**
	 * @param campaignConfigPresenter
	 */
	public void initCampaignConfigView(
			CampaignConfigPresenter campaignConfigPresenter) {
		campaignConfigPresenter.addCampaignSummaryDTO(campaignSummaryDTO);
		campaignConfigPresenter.initView();
	}

	private void initCharactersInCampaignPanel() {

		campaignService.getCampaignCharacters(campaignSummaryDTO.getId(),
				new SuccessCallback<Result<List<CharacterHeaderDTO>>>() {
					@Override
					public void onSuccess(
							Result<List<CharacterHeaderDTO>> result) {
						final List<CharacterHeaderDTO> characterHeaderDTOList = result
								.getData();
						setCharacterHeaderDTOList(characterHeaderDTOList);
						createCharacterInCampaignPresenter(characterHeaderDTOList);
					}

				});

	}

	/**
	 * @param characterHeaderDTOList
	 */
	protected void createCharacterInCampaignPresenter(
			final List<CharacterHeaderDTO> characterHeaderDTOList) {
		charactersInCampaignPresenter = eventBus
				.addHandler(CharactersInCampaignPresenter.class);
		charactersInCampaignPresenter
				.addCharacterHeaderList(characterHeaderDTOList);
		charactersInCampaignPresenter.addCampaignSummaryDTO(campaignSummaryDTO);
		charactersInCampaignPresenter.initView();
		listContents.put(CampaignPanel.CHARACTERS_LIST,
				charactersInCampaignPresenter.getView().asWidget());
		initCharacterDTOsMap(characterHeaderDTOList);
	}

	protected void initCharacterDTOsMap(
			List<CharacterHeaderDTO> characterHeaderDTOList) {
		if (characterHeaderDTOList != null) {
			for (CharacterHeaderDTO characterHeaderDTO : characterHeaderDTOList) {
				characterHeaderDTOsMap.put(characterHeaderDTO.getId(),
						characterHeaderDTO);
			}
		}
	}

	/**
	 * 
	 */
	private void initNotificationPanel() {
		List<NotificationDTO> listNotificationDTOs = campaignSummaryDTO
				.getNotifications();
		this.view.addCampaignName(campaignSummaryDTO.getName());
		if (listNotificationDTOs != null && !listNotificationDTOs.isEmpty()) {
			for (NotificationDTO notificationDTO : campaignSummaryDTO
					.getNotifications()) {
				this.view.addNotification(notificationDTO.getMessage(),
						notificationDTO.getDate());
			}
		}
	}

	public void onSetNickName(String nickName) {
		this.nickName = nickName;
	}
	public void onSetUserId(long userId) {
		this.userId = userId;
	}
	public String getCampaignName() {
		return campaignSummaryDTO.getName();
	}

	public void refreshContent() {
		this.view.setSelectedContent(selectedContent);
		this.view.refreshContent(listContents.get(selectedContent));
	}

	public void addCampaignSummary(CampaignSummaryDTO campaignSummaryDTO) {
		this.campaignSummaryDTO = campaignSummaryDTO;

	}

	public CampaignSummaryDTO getCampaignSummaryDTO() {
		return this.campaignSummaryDTO;
	}

	public void setSelectedContent(String selectedContent) {
		this.selectedContent = selectedContent;
	}

	protected boolean isOwner(Long  ownerId) {
		if (ownerId != null)
			return userId==ownerId;
		return false;
	}


	protected boolean isGameMaster() {
		return nickName.equals(campaignSummaryDTO.getGameMasterNickname());
	}

	public void setCharacterHeaderDTOList(
			List<CharacterHeaderDTO> characterHeaderDTOList) {
		this.characterHeaderDTOList = characterHeaderDTOList;
	}

	public void addNewNPCInCampaign(CharacterHeaderDTO characterHeaderDTO) {
		if (characterHeaderDTOList == null) {
			characterHeaderDTOList = new ArrayList<CharacterHeaderDTO>();
		}
		this.characterHeaderDTOList.add(characterHeaderDTO);
		characterHeaderDTOsMap.put(characterHeaderDTO.getId(),
				characterHeaderDTO);
		charactersInCampaignPresenter.addNewNPCInCampaign(characterHeaderDTO);
	}

	public void initCharacterDescriptionPopup(final Long characterId) {

		campaignService.getCharacterDescription(campaignSummaryDTO.getId(),
				characterId, null, null,
				new SuccessCallback<Result<CharacterDescriptionDTO>>() {

					@Override
					public void onSuccess(Result<CharacterDescriptionDTO> result) {
						CharacterHeaderDTO characterHeaderDTO = characterHeaderDTOsMap
								.get(characterId);
						if (characterHeaderDTO != null) {
							CharacterCDO characterCDO = new CharacterCDO(
									characterHeaderDTOsMap.get(characterId),
									result.getData());
							CharacterDescriptionPresenter characterDescriptionPresenter = eventBus
									.addHandler(CharacterDescriptionPresenter.class);
							characterDescriptionPresenter.setByCharacterId(null);
							characterDescriptionPresenter
									.initCharacterDescriptionPopup(
											characterCDO,
											isOwner(characterHeaderDTO
													.getOwner()),
											isGameMaster(),
											characterHeaderDTOList);
						}
					}
				});
	}

	public void refreshCampaignInfos() {
		campaignConfigPresenter.addCampaignSummaryDTO(campaignSummaryDTO);
		campaignConfigPresenter.refreshCampaignInfos();
		this.view.refreshContent(campaignConfigPresenter.getView());
	}

	public boolean isViewInitialized() {
		// TODO Auto-generated method stub
		return isInit;
	}

	public void display(){
		eventBus.addCampaignTab(view, campaignSummaryDTO.getName(),
				campaignSummaryDTO.getId());
		
	}

}
